<?php

$space = Blyn::app()->getActiveSpace();
$model = new BDynamicFormModel();
$model->addField('oldPassword', NULL, Yii::t('site', 'Old Password').':');
$model->addField('newPassword', NULL, Yii::t('site', 'New Password').':');
$model->addField('confirmNewPassword', NULL, Yii::t('site', 'Confirm New Password').':');
$isMyIdExist = TRUE;
$isSaved = FALSE;

$screen = Blyn::app()->getUIManager()->getCurrentScreen();
$webUser = yii::app()->user;
if ($webUser != NULL)
    $currentUser = new BUser($webUser->getId());
            
if ($screen->isAjaxLinkClicked('changePassword')) {
    if (!empty($_POST) && isset($_POST['BDynamicFormModel'])) {
        $model->attributes = $_POST['BDynamicFormModel'];
        if($model->oldPassword === $model->newPassword)
        {
            echo "<div class='op-alert'><font color='#FF0000'>新旧密码相同，请重新输入!</font></div><div id='dialog-mask'></div>";
        }
        else if($model->newPassword !== $model->confirmNewPassword)
        {
            echo "<div class='op-alert'><font color='#FF0000'>两次输入的新密码不同，请重新输入!</font></div><div id='dialog-mask'></div>";
            
        }
        else{
            $errorCode = $currentUser->authenticate($currentUser->getUserId(), $model->oldPassword);

            if ($errorCode === BUserIdentity::ERROR_NONE) {
                $attributes = array();

            $attributes['password'] =  $model->newPassword;
                $currentUser->save($attributes);
                $isSaved = true;
            } else  {
                echo "<div class='op-alert'><font color='#FF0000'>旧密码不正确，请重新输入!</font></div><div id='dialog-mask'></div>";
            }
        }
    }
}

//
//$model->spaceId = $space->getId();
//$model->spaceName = $space->getName();
//if ($space->myId != NULL)
//    $model->myId = $space->myId;
?>
<div class="form">
    <?php echo CHtml::beginForm(); ?>

    <?php echo CHtml::errorSummary($model); ?>
    <?php if ($isSaved) { ?>
    <div class="well text-danger"><font color='#008000'>密码修改成功!</font></div>
    <?php } ?>

    <div class="row">
        <?php echo CHtml::activeLabel($model, 'oldPassword'); ?>
        <?php echo CHtml::activePasswordField($model, 'oldPassword',array(
            'size' => 32,
        )); ?>
    </div>

    <div class="row">
        <?php echo CHtml::activeLabel($model, 'newPassword'); ?>
        <?php echo CHtml::activePasswordField($model, 'newPassword', array(
            'size' => 32,
        )); ?>       
    </div>

    <div class="row">
        <?php echo CHtml::activeLabel($model, 'confirmNewPassword'); ?>
        <?php echo CHtml::activePasswordField($model, 'confirmNewPassword' ,array(
            'size' => 32,
        )) ?>
    </div>  

    <div class="row submit">
        <?php echo $screen->getAjaxLink(array('name' => yii::t('site', 'Modify'), 'id' => 'changePassword'), array('htmlOptions' => array('class' => 'btn'))); ?>       
    </div>    

    <?php echo CHtml::endForm(); ?>
</div><!-- form -->
